package cn.itcast.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Menu;
import cn.itcast.service.MenuService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
@CrossOrigin
public class MenuController {
    @Reference
    private MenuService menuService;

    /**
     * 保存权限信息
     *
     * @param menu
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Menu menu, Integer[] permissionIds) {
        try {
            menuService.add(menu, permissionIds);
            return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
        }
    }

    /**
     * parentMenuId
     * 分页查询权限信息
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping(path = "/page")
    public Result findPage(QueryPageBean queryPageBean) {
        //分页查询权限信息
        try {
            PageResult pageResult = menuService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    /**
     * 根据id查询菜单详情
     *
     * @param id
     * @return
     */
    @GetMapping(path = "/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        try {
            Menu menu = menuService.findById(id);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    /**
     * 查询菜单管理权限信息
     *
     * @param permissionId
     * @return
     */
    @GetMapping(path = "/{id}/permissions")
    public Result selectedPermissionId(@PathVariable("id") Integer permissionId) {
        try {
            Integer[] permissionIds = menuService.selectedPermissionId(permissionId);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * 更新权限信息
     *
     * @param menu
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Menu menu, Integer[] permissionIds) {
        try {
            menuService.update(menu, permissionIds);
            return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.EDIT_PERMISSION_FAIL);
        }
    }

    /**
     * 删除菜单信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(path = "/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        try {
            menuService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
        }
    }

    /**
     * 查询所有套餐信息
     *
     * @return
     */
    @GetMapping
    public Result findAll() {
        try {
            List<Menu> menus = menuService.findAll();
            return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS, menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
    }
}